<?php


namespace Home\Controller;

use Faker\Factory;
use Think\Controller;

class UsercenterController extends Controller
{

    /**
     *  *个人中心
     */
    //个人中心
    public function index()
    {
        $faker = Factory::create();
        $myInfo = [
            "nickname" => $faker->lastName,
            "account" => $faker->ean13,
            "email" => $faker->email,
            "sign" => $faker->imageUrl(150, 25),
            "department" => $faker->word,
            "level" => $faker->word,
            "post" => $faker->word,
            "report" => $faker->lastName,
            "state" => $faker->word,
            "entry_date" => $faker->unixTime,
            "contract_period" => $faker->unixTime
        ];
        $myInfo["work_age"] = time() - $myInfo["entry_date"];

        $this->assign("myInfo", $myInfo);
        $this->display();
    }

    //自定义快捷方式
    public function shortcut()
    {
        $faker = Factory::create();
        $shortcut = [];
        for ($i = 0; $i < $faker->numberBetween(20, 40); $i++) {
            //循环依次给该数组赋值一套数据
            $shortcut[] = [
                'id' => $faker->randomNumber(),
                'icon' => $faker->imageUrl(30, 30),
                'name' => $faker->lastName,
                'checked' => $faker->numberBetween(1, 2)//该快捷方式是否是用户本身已经有的快捷方式
            ];
        }

        $this->assign('shortcut', $shortcut);
        $this->display();
    }

    //授权首页
    public function authorization()
    {
        $faker = Factory::create();
        $from_me = [];
        $to_me = [];
        for ($i = 0; $i < $faker->numberBetween(1, 10); $i++) {
            //循环依次给该数组赋值一套数据
            $from_me[] = [
                'number' => $faker->numerify('number######'),
                'status' => $faker->numberBetween(-1, 2),
                'module' => $faker->randomElement(['ECO', 'Review', '审批']),
                'user' => 'myName',
                'auth_user' => $faker->lastName,
                'from' => $faker->unixTime,
                'to' => $faker->unixTime,
                'create_time' => $faker->unixTime,
            ];
        }
        for ($i = 0; $i < $faker->numberBetween(1, 10); $i++) {
            //循环依次给该数组赋值一套数据
            $to_me[] = [
                'number' => $faker->numerify('number######'),
                'status' => $faker->numberBetween(-1, 2),
                'module' => $faker->randomElement(['ECO', 'Review', '审批']),
                'user' => $faker->lastName,
                'auth_user' => 'myName',
                'from' => $faker->unixTime,
                'to' => $faker->unixTime,
                'create_time' => $faker->unixTime,
            ];
        }

        $this->assign('from_me', $from_me);
        $this->assign('to_me', $to_me);
        $this->display('Usercenter/authorization/index');
    }

    //创建授权
    public function authorizationCreate()
    {
        $faker = Factory::create();
        $auth = [];
        for ($i = 0; $i < 10; $i++) {
            //循环依次给该数组赋值一套数据
            $auth[] = [
                'auth_user_id' => $faker->randomNumber(),
                'nickname' => $faker->lastName
            ];
        }
//        $auth['create_time'] = time();
        $this->assign('auth', $auth);
        $this->display('Usercenter/authorization/create');
    }

    //授权详情
    public function authorizationShow()
    {
        $faker = Factory::create();
        $auth = [
            'number' => $faker->numerify('number######'),
            'status' => $faker->numberBetween(-1, 2),
            'user' => $faker->lastName,
            'create_time' => $faker->unixTime,
            'id' => $faker->randomDigit,
        ];
        for ($i = 0; $i < $faker->numberBetween(1, 3); $i++) {
            $auth['details'][] = [
                'module' => $faker->randomElement(['ECO','Review','审批']),
                'auth_action' => $faker->randomElement(['发起评审','评审','撤回','编辑','删除']),
                'auth_user' => $faker->lastName,
                'from' => $faker->unixTime,
                'to' => $faker->unixTime,
                'remark' => $faker->text(100)
            ];
        }
        //操作情况详情
        $actions = [];
        for ($i = 0; $i< $faker->numberBetween(1,3); $i++) {
            $actions[] = [
                'no' => $faker->numerify('ECO######')
            ];
        }
        //foreach 循环为数组赋值，&符号表示引用，为该变量本身赋值，若不使用则只是copy了一份数据到副本里，并没有赋值成功
        foreach ($actions as $key => &$action) {
            //给levels数组中每一个子数据新添一个属性reviewers
            for ($i = 0; $i < $faker->numberBetween(1,5); $i++) {
                $action['actions'][] = [
                    'nickname' => $faker->lastName,
                    'action' => $faker->randomElement(['发起评审','评审','撤回','编辑','删除']),
                    'opinion' => $faker->randomElement(['ok','']),
                    'time' => $faker->unixTime
                ];
            }
        }

        $this->assign('auth', $auth);
        $this->assign('actions',$actions);
        $this->display('Usercenter/authorization/show');
    }

    //授权编辑
    public function authorizationEdit()
    {
        $faker = Factory::create();
        for ($i = 0; $i < $faker->numberBetween(1, 3); $i++) {
            $auth[] = [
                'module' => $faker->randomElement(['ECO', 'Review', '审批']),
                'auth_action' => $faker->randomElement(['review', 'send', 'pull', 'del', 'edit']),
                'auth_user_id' => $faker->randomNumber(),
                'auth_user' => $faker->lastName,
                'from' => $faker->unixTime,
                'to' => $faker->unixTime,
                'remark' => $faker->text(100)
            ];
        }

        $users = [];
        for ($i = 0; $i < 10; $i++) {
            //循环依次给该数组赋值一套数据
            $users[] = [
                'auth_user_id' => $faker->randomNumber(),
                'nickname' => $faker->lastName
            ];
        }

        $this->assign('auth', $auth);
        $this->assign('users', $users);
        $this->display('Usercenter/authorization/edit');
    }
}